package com.fsc.fragment.income;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.fsc.fragment.DBHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * 是实体Income的Dao
 * @author fsc
 *
 */
public class InComeDao {
	//获取系统当前时间
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	String date = dateFormat.format(new java.util.Date());
	
	private DBHelper dbHelper;
	
	
	//重写构造方法
	public InComeDao(Context context){
		dbHelper = new DBHelper(context);
	}
	
	//新增
	public void insert(InCome income){
		//得到连接
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		
		//调用方法
			//执行insert into InCome(money,time,type,handler,mark)values(x,'','','','')
		ContentValues values = new ContentValues();
		values.put("money", income.getMoney());
		values.put("time", income.getTime());
		values.put("type", income.getType());
		values.put("handler", income.getHandler());
		values.put("mark", income.getMark());
		long id = database.insert("InCome", null, values);
		Log.i("TAG", "id="+id);
		
		income.setId((int) id);
		income.setTime(date);
		//关闭连接
		database.close();
	}
	
	//删除
	public void delete(int id){
		//得到连接
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		
		//调用方法delete from InCome where _id = ?
		int deleteCount = database.delete("InCome", "_id=?", new String[]{id+""});
		Log.i("TAG", "deleteCount="+deleteCount);
		
		//关闭连接
		database.close();
	}
	
	//更新
	public void update(InCome income){
		//得到连接
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		
		//调用方法update InCome set money=?,time=?...where _id=?
		ContentValues values = new ContentValues();
		values.put("money", income.getMoney());
		values.put("time", income.getTime());
		values.put("type", income.getType());
		values.put("handler", income.getHandler());
		values.put("mark", income.getMark());
		database.update("InCome", values , "_id="+income.getId(), null);
		
		income.setTime(date);
		//关闭连接
		database.close();
	}
	
	//查询
	public List<InCome> query(){

		//得到连接
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		
		//得到list对象
		List<InCome> list = new ArrayList<InCome>();
		
		//调用方法select * from InCome
		Cursor cursor = database.query("InCome", null, null, null, null, null, "_id desc");
		//4.从Cursor中取出所以数据封装到List<InCome>中
		while(cursor.moveToNext()){
			int id = cursor.getInt(0);
			double money = cursor.getDouble(1);
			String time = cursor.getString(2);
			String type = cursor.getString(3);
			String handler = cursor.getString(4);
			String mark = cursor.getString(5);
			
			//保存到list中
			list.add(new InCome(id,money,time,type,handler,mark));
		}
		
		cursor.close();
		//关闭连接
		database.close();
		return list;
	}

	//根据id查询收入
	public List<InCome> query(int id){
		List<InCome> list = new ArrayList<InCome>();
		//得到连接 select * from OutCome
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		//调用方法
		Cursor cursor = database.query("InCome", null, "_id=?", new String[] {id+""}, null, null, null);
		//从cursor中获取数据封装到list中
		while(cursor.moveToNext()){
			int _id=cursor.getInt(0);
			double money = cursor.getDouble(1);
			String time = cursor.getString(2);
			String type = cursor.getString(3);
			String handler = cursor.getString(4);
			String mark = cursor.getString(5);
			
			list.add(new InCome(_id,money,time,type,handler,mark));
		}
		//关闭连接
		cursor.close();
		database.close();
		
		//返回list
		return list;
	}
}
